package com.finger.forest.base;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import org.litepal.LitePalApplication;

import java.util.Stack;


/**
 * @author sy
 * @date 2020/6/7
 * @desc
 */
public class BaseApplication extends LitePalApplication implements Application.ActivityLifecycleCallbacks {

    private static final String TAG = BaseApplication.class.getSimpleName();
    private static BaseApplication instance;
    private Stack<Activity> activities = new Stack<>();

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;
        registerActivityLifecycleCallbacks(this);
        /*// 环信的easeUi
        EaseUI.getInstance().init(this, null);

        SpeechUtility.createUtility(this, "appid=5f0330bc");

        // 以下语句用于设置日志开关（默认开启），设置成false时关闭语音云SDK日志打印
        Setting.setShowLog(false);
        TTSUtils.getInstance().init();

        CustomCameraAgent.init(this);// 自定义相机

        initCloudChannel(this);

        UdeskSDKManager.getInstance().initApiKey(this, "1892368.udesk.cn", "4f0383cf1391942442c0a7b798ac5508", "6c36a3bd06959a21");*/

        // 友盟
        UMConfigure.setLogEnabled(true);
        // 初始化SDK
        UMConfigure.init(this, "606c046fde41b946ab3e7392", "xunhuxitong", UMConfigure.DEVICE_TYPE_PHONE, null);
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);

    }

    public static BaseApplication getInstance() {
        return instance;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        if (!activities.contains(activity)) {
            activities.add(activity);
        }
    }

    public Stack<Activity> getActivities() {
        return activities;
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        activities.remove(activity);
    }

    /**
     * 初始化云推送通道
     *
     * @param
     */
    /*private void initCloudChannel(Context applicationContext) {
        this.createNotificationChannel();
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
//                Log.d(TAG, "init cloudchannel success");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }*/

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // 通知渠道的id
            String id = "1";
            // 用户可以看到的通知渠道的名字.
            CharSequence name = "逸路安司机";
            // 用户可以看到的通知渠道的描述
            String description = "逸路安司机端";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            // 配置通知渠道的属性
            mChannel.setDescription(description);
            // 设置通知出现时的闪灯（如果 android 设备支持的话）
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            // 设置通知出现时的震动（如果 android 设备支持的话）
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            //最后在notificationmanager中创建该通知渠道
            mNotificationManager.createNotificationChannel(mChannel);
        }
    }
}
